package com.myf.services;

import com.myf.AppException;
import com.myf.PaginationResult;
import com.myf.Utility;
import com.myf.dtos.SelectItem;
import com.myf.dtos.warehouse.WarehouseInput;
import com.myf.dtos.warehouse.WarehouseQueryInput;
import com.myf.dtos.warehouse.WarehouseQueryOutput;
import com.myf.entities.WarehouseEntity;
import com.myf.mappers.WarehouseMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class WarehouseService {

    private final WarehouseMapper warehouseMapper;

    public WarehouseService(WarehouseMapper warehouseMapper) {
        this.warehouseMapper = warehouseMapper;
    }

    public int save(WarehouseInput warehouseInput, int currentUserId) throws AppException {
        if (warehouseInput.id == null || warehouseInput.id == 0) {
            // 检查名称是否重复
            var map = new HashMap<String, Object>();
            map.put("name", warehouseInput.name);
            if (warehouseMapper.isNameExists(map) == 1) {
                throw new AppException("name ".concat(warehouseInput.name).concat(" already exists"));
            }
            // 插入数据库
            var entity = new WarehouseEntity();
            entity.attributes = "{}";
            setToEntity(warehouseInput, entity);
            entity.createAt = Utility.getCurrentUTCTime();
            entity.createBy = currentUserId;
            entity.version = 0;
            return warehouseMapper.insert(entity);
        } else {
            // 检查名称是否重复
            var map = new HashMap<String, Object>();
            map.put("id", warehouseInput.id);
            map.put("name", warehouseInput.name);
            if (warehouseMapper.isNameExists(map) == 1) {
                throw new AppException("name ".concat(warehouseInput.name).concat(" already exists"));
            }
            // 更新数据库
            var entity = this.warehouseMapper.selectById(warehouseInput.id);
            if (entity == null) {
                throw new AppException("the warehouse info is invalid");
            }
            setToEntity(warehouseInput, entity);
            entity.updateAt = Utility.getCurrentUTCTime();
            entity.updateBy = currentUserId;
            return warehouseMapper.updateById(entity);
        }
    }

    private static void setToEntity(WarehouseInput warehouseInput, WarehouseEntity entity) {
        entity.code = warehouseInput.code;
        entity.name = warehouseInput.name;
        entity.address = warehouseInput.address;
        entity.buildingStructure = warehouseInput.buildingStructure;
        entity.category = warehouseInput.category;
        entity.contactMobileNo = warehouseInput.contactMobileNo;
        entity.contactPeople = warehouseInput.contactPeople;
        entity.fireProtectionRating = warehouseInput.fireProtectionRating;
        entity.fireResistanceRating = warehouseInput.fireResistanceRating;
        entity.height = warehouseInput.height;
        entity.width = warehouseInput.width;
        entity.length = warehouseInput.length;
        entity.status = warehouseInput.status;
    }

    public PaginationResult<WarehouseQueryOutput> query(WarehouseQueryInput input) {
        var map = new HashMap<String, Object>();
        if (!input.keyword.isBlank()) {
            map.put("keyword", "%".concat(input.keyword).concat("%"));
        }
        map.put("limit", input.pageSize);
        map.put("offset", input.getOffset());
        var total = warehouseMapper.getPaginationCount(map);
        var rows = warehouseMapper.getPaginationList(map);
        return new PaginationResult<>(total, rows);
    }

    public WarehouseEntity getById(int id) {
        return warehouseMapper.selectById(id);
    }

    /// 删除
    public int delete(List<Integer> ids, int currentUserId) {
        var hashMap = new HashMap<String, Object>();
        hashMap.put("ids", ids);
        hashMap.put("deleteBy", currentUserId);
        hashMap.put("deleteAt", Utility.getCurrentUTCTime());
        return warehouseMapper.removeByIds(hashMap);
    }

    public List<SelectItem> getForSelectItems(String keyword) {
        if (keyword != null && !keyword.isBlank()) {
            keyword = "%" + keyword + "%";
        }
        return warehouseMapper.getForSelectItems(keyword);
    }
}
